﻿using System;
using NMock2;
using NMock2.Events;


namespace NMock3.Tests
{
	[NUnit.Framework.TestFixture]
	public class _FunctionalTests
	{
		private Mock<IView> _view;
		private Mock<IModel> _model;
		MockFactory factory = new MockFactory();


		[NUnit.Framework.SetUp]
		public void TestInit()
		{
			_view = factory.CreateMock<IView>();
			_model = factory.CreateMock<IModel>();

		}

		[NUnit.Framework.TearDown]
		public void TestClean()
		{
			factory.VerifyAllExpectationsHaveBeenMet();
		}

		[NUnit.Framework.Test]
		[NUnit.Framework.ExpectedException(typeof(ArgumentNullException))]
		public void ConstructorTest1()
		{
			new Presenter(null, _model.MockObject);
		}

		[NUnit.Framework.Test]
		[NUnit.Framework.ExpectedException(typeof(ArgumentNullException))]
		public void ConstructorTest2()
		{
			new Presenter(_view.MockObject, null);
		}

		[NUnit.Framework.Test]
		public void SearchTest_OldWay()
		{
			MockEventInvoker searchEventInvoker = new MockEventInvoker();
			MockExpectationHelper<IView> helper = new MockExpectationHelper<IView>(_view.MockObject);
			helper.GetEventExpectation("Search").Will(searchEventInvoker.Hookup());

			Presenter p = new Presenter(_view.MockObject, _model.MockObject);

			Expect.Once.On(_view.MockObject).GetProperty("SearchText").Will(Return.Value("SomeText"));
			Expect.Once.On(_model.MockObject).Method("GetCount").With("SomeText").Will(Return.Value(3));
			Expect.Once.On(_view.MockObject).SetProperty("Count").To(3);

			searchEventInvoker.Invoke();
		}

		[NUnit.Framework.Test]
		public void SearchTest1()
		{
			MockEventInvoker searchEventInvoker = _view.Expects.One.EventBinding(v => v.Search += null);

			Presenter p = new Presenter(_view.MockObject, _model.MockObject);
			_view.Expects.One.GetProperty(v => v.SearchText).Will(Return.Value("SomeText"));
			_model.Expects.One.Method(m => m.GetCount("IGNORED PARAM")).With("SomeText").Will(Return.Value(3));
			_view.Expects.One.SetProperty(v => v.Count).To(3);

			searchEventInvoker.Invoke();
		}

		[NUnit.Framework.Test]
		public void SearchTest2()
		{
			MockEventInvoker searchEventInvoker = _view.Expects.One.EventBinding(v => v.Search += null);

			Presenter p = new Presenter(_view.MockObject, _model.MockObject);

			_view.Expects.One.GetProperty(v => v.SearchText).Will(Return.Value("SomeText"));

			//using alternate method
			_model.Expects.One.MethodWith(m => m.GetCount("SomeText")).Will(Return.Value(3));

			//using alternate method
			_view.Expects.Exactly(1).SetPropertyTo(v => v.Count = 3);

			searchEventInvoker.Invoke();
		}
	}
}
